package dk.au.cs.bdsi.mail.activities;

import dk.au.cs.bdsi.core.activities.DeleteException;
import dk.au.cs.bdsi.core.representation.Identifier;
import dk.au.cs.bdsi.core.representation.ServiceUri;
import dk.au.cs.bdsi.mail.domain.Mail;
import dk.au.cs.bdsi.mail.domain.MailStatus;
import dk.au.cs.bdsi.mail.repositories.MailRepository;
import dk.au.cs.bdsi.mail.representation.MailRepresentation;

public class DeleteMailActivity {
	// TODO Make concurrent
	public MailRepresentation delete(ServiceUri requestUri) {
		// Retrieve message
		Identifier id = requestUri.getId();
		Mail message = MailRepository.current().get(id);
		
		// Ensure it exists and can be deleted
		if (message == null) throw new NoSuchMailException();
		MailStatus status = message.getStatus();
		if (status != MailStatus.Sent && status != MailStatus.NotSent)
			throw new DeleteException();
		
		// Okay to delete
		MailRepository.current().remove(id);
		return new MailRepresentation(message);
	}
}
